package library.gui.component;

import java.util.HashMap;
import java.util.Map;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.HBox;

public class SearchPane extends HBox{

	private FunctionalButton button, clear;
	private HBox texts = new HBox(), options = new HBox();
	private Map<String, Object> map = new HashMap<>();
	
	public SearchPane() {

		this.setAlignment(Pos.CENTER_RIGHT);
		
		this.button = new FunctionalButton("筛选", null);
		this.clear = new FunctionalButton("清空", ()->{
					texts.getChildren().forEach((x)->{
						((InputLine)x).clear();
					});
					options.getChildren().forEach((x)->{
						((CheckBox)x).setSelected(false);
					});
				});

		this.setPadding(new Insets(5, 5, 5, 5));
		this.setSpacing(20);
//		this.setStyle("-fx-border-style: outset; -fx-border-color: #98bf21; -fx-border-width: 5px");
		this.setStyle("-fx-border-style: solid; -fx-border-color: silver");
		options.setSpacing(10);
		
		this.getChildren().add(texts);
		this.getChildren().add(options);
		this.getChildren().add(clear);
		this.getChildren().add(button);
		
	}
	
	public void listenSearch(Runnable runnable) {
		this.button.setFunction(runnable);
		this.clear.setFinal(runnable);
	}

	public SearchPane addTextKey(String key) {
		InputLine inputLine = new InputLine(key, false);
		inputLine.setPrefWidth(220);
		inputLine.setPadding(new Insets(0, 10, 0, 0));
		texts.getChildren().add(inputLine);
		map.put(key, inputLine);
		return this;
	}
	
	public SearchPane addOptionKey(String key) {
		CheckBox e = new CheckBox(key);
		e.setPadding(new Insets(3, 0, 0, 0));
		options.getChildren().add(e);
		map.put(key, e);
		return this;
	}
	
	public String getText(String key) {
		InputLine c = ((InputLine)map.get(key));
		return c == null ? "" : ((InputLine)map.get(key)).getValue();
	}
	
	public boolean isSelected(String key) {
		CheckBox c = ((CheckBox)map.get(key));
		return c == null ? false : c.isSelected();
	}
	
}
